﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace EasyPiano
{
    public class Song
    {
        //音符与歌词
        public List<MusicScore> hand1 = new List<MusicScore>();
        public List<MusicScore> hand2 = new List<MusicScore>();
        public List<string> words = new List<string>();
        //歌曲信息
        private int handCount = 1;
        public int offsetIndex = 0;
        public int unitTime = 250;
        private bool hasWord;
        public string songName;
        public string musicAuthor;//曲作者
        public string wordAuthor;//词作者

        public int HandCount
        {
            get => handCount;
            set
            {
                handCount = value;
                if (handCount == 1)
                {
                    hand2.Clear();
                }                   
                else if(handCount==2)
                {
                    for (int i = 0; i < hand1.Count; i++)
                        hand2.Add(new MusicScore(0, hand1[i].duration, 2));
                }
            }
        }

        public bool HasWord
        {
            get => hasWord;
            set
            {
                hasWord = value;
                if (hasWord)
                {
                    for (int i = 0; i < hand1.Count; i++)
                        words.Add("");
                }
                else
                {
                    words.Clear();
                }
            }
        }

        public void Add(int index1, int index2, float delay,string word)
        {
            hand1.Add(new MusicScore(index1, delay, 1));
            if (HandCount == 2)
            {
                hand2.Add(new MusicScore(index2, delay, 2));
            }
            if (HasWord)
                this.words.Add(word);
        }
        public void Change(int i, int index1, int index2, float delay, string word)
        {
            hand1[i] = new MusicScore(index1, delay, 1);
            if(HandCount==2)
            {
                hand2[i] = new MusicScore(index2, delay, 2);
            }
            if (HasWord)
                this.words[i] = word;
        }
        public void Remove(int i)
        {
            hand1.RemoveAt(i);
            if(HandCount==2)
            {
                hand2.RemoveAt(i);
            }
            if (hasWord)
                words.RemoveAt(i);
        }
        public void Insert(int i, int index1, int index2, float delay, string word)
        {
            hand1.Insert(i, new MusicScore(index1, delay, 1));
            if (HandCount == 2)
            {
                hand2.Insert(i, new MusicScore(index2, delay, 2));
            }
            if (HasWord)
                this.words.Add(word);
        }
        public void Save(string path)
        {
            //创建XML文档对象
            XmlDocument doc = new XmlDocument();
            //创建第一行描述信息，并且添加到doc文档中
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //创建根节点
            XmlElement music = doc.CreateElement("Music");
            music.SetAttribute("SongName", songName);
            music.SetAttribute("MusicAuthor", musicAuthor);
            music.SetAttribute("WordAuthor", wordAuthor);
            music.SetAttribute("UnitTime", unitTime.ToString());
            music.SetAttribute("HandCount", HandCount.ToString());
            music.SetAttribute("OffsetIndex", offsetIndex.ToString());
            music.SetAttribute("HasWord", HasWord.ToString());
            //将根节点添加到文档中
            doc.AppendChild(music);

            for (int i = 0; i < hand1.Count; i++)
            {
                //给根节点创建第一个子节点
                XmlElement score = doc.CreateElement("Score");
                score.SetAttribute("String", hand1[i].ScoreString);
                score.SetAttribute("DelayTime", hand1[i].duration.ToString());
                score.SetAttribute("Hand", "1");
                //添加到根节点
                music.AppendChild(score);

                if (HandCount == 2)
                {
                    XmlElement score1 = doc.CreateElement("Score");
                    score1.SetAttribute("String", hand2[i].ScoreString);
                    score1.SetAttribute("DelayTime", hand2[i].duration.ToString());
                    score1.SetAttribute("Hand", "2");
                    //添加到根节点
                    music.AppendChild(score1);
                }

                if(HasWord)
                {
                    XmlElement word = doc.CreateElement("Word");
                    word.InnerText = words[i];
                    //添加到根节点
                    music.AppendChild(word);
                }
            }

            doc.Save(path);
        }
        public void Read(string path)
        {
            XmlDocument doc = new XmlDocument();
            //加载要读取的XML
            doc.Load(path);
            //获得根节点
            XmlElement music = doc.DocumentElement;
            songName = music.Attributes["SongName"]?.Value;
            musicAuthor = music.Attributes["MusicAuthor"]?.Value;
            wordAuthor = music.Attributes["WordAuthor"]?.Value;
            try
            {
                unitTime = int.Parse(music.Attributes["UnitTime"]?.Value);
                HandCount = int.Parse(music.Attributes["HandCount"]?.Value);
                offsetIndex = int.Parse(music.Attributes["OffsetIndex"]?.Value);
                HasWord = bool.Parse(music.Attributes["HasWord"]?.Value);
            }
            catch
            {

            }
            finally
            {
                //获得子节点 返回节点的集合
                XmlNodeList nodes = music.ChildNodes;
                for (int i = 0; i < nodes.Count; i++)
                {
                    string str1 = nodes[i].Attributes["String"].Value;
                    float delay1 = float.Parse(nodes[i].Attributes["DelayTime"].Value);
                    hand1.Add(new MusicScore(str1, delay1, 1));
                    if (HandCount == 2)
                    {
                        i++;
                        string str2 = nodes[i].Attributes["String"].Value;
                        float delay2 = float.Parse(nodes[i].Attributes["DelayTime"].Value);
                        hand2.Add(new MusicScore(str2, delay2, 2));
                    }
                    if(HasWord)
                    {
                        i++;
                        words.Add(nodes[i].InnerText);
                    }
                }
            }            
        }
    }
}
